package ru.ifmo.tanya.keepers;

import ru.ifmo.tanya.core.users.User;
import ru.ifmo.tanya.core.users.UserState;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * User: nikart
 * Date: 1/27/13
 * Time: 4:06 AM
 */
public class UserStateKeeper {

    private final static ConcurrentMap<User, UserState> user2state = new ConcurrentHashMap<>();

    private final static ConcurrentHashMap<User, Long> statusTime = new ConcurrentHashMap<>();

    public void updateState(User user, UserState state) {
        user2state.put(user, state);
        statusTime.put(user, System.currentTimeMillis());
    }

    public UserState getUserState(User user) {
        UserState state = user2state.get(user);
        if (state == null) {
            user2state.put(user, UserState.AVAILABLE);
            statusTime.put(user, System.currentTimeMillis());
            return UserState.AVAILABLE;
        }
        return state;
    }

    public Long getStatusTime(User user) {
        Long time = statusTime.get(user);
        if (time != null) {
            return time;
        } else {
            return System.currentTimeMillis();
        }
    }
}
